package com.igeek.d0614.test2;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: zhiyu
 * @Date:18:03
 * @DESC:
 */
public class AtomicIntegerDemo {
    private static final Lock lock = new ReentrantLock();
    private static int num = 0;
    public static void atoInteger(){
        try {
            lock.lock();
            while (num<10){
                AtomicInteger count = new AtomicInteger(1);
                if (count.compareAndSet(1,2)==true){
                    System.out.print("A");
                }
                if (count.compareAndSet(2,3)==true){
                    System.out.print("B");
                }
                if (count.compareAndSet(3,4)==true){
                    System.out.println("C");
                    num++;
                }
                if (count.compareAndSet(4,5)==true){
                    System.out.print("A");
                }
                if (count.compareAndSet(5,6)==true){
                    System.out.print("B");
                }
                if (count.compareAndSet(6,1)==true){
                    System.out.println("C");
                    num++;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                atoInteger();
            },"线程1"+i).start();
        }
    }

}
